/**
 * Copyright (C) 2015-2016 Guangzhou Xunhong Network Technology Co., Ltd. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jxccp.ui.view.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.jxccp.im.chat.common.entity.JXCommonQuestion;
import com.jxccp.ui.R;
import com.jxccp.ui.entities.JXOrderInfo;
import com.jxccp.ui.presenter.JXChatPresenter;
import com.jxccp.ui.view.JXBaseChatFragment;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class JXCommonQuestionListAdapter extends JXBasicAdapter<JXCommonQuestion, ListView> {

    private LayoutInflater mInflater;

    private Set<String> openQuestionCache = new HashSet<>();

    private JXChatPresenter chatPresenter;

    public JXCommonQuestionListAdapter(Context context, List<JXCommonQuestion> list, JXChatPresenter chatPresenter) {
        super(context, list);
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.chatPresenter = chatPresenter;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.jx_common_question_item, null);
        }
        final JXCommonQuestion commonQuestion = list.get(position);
        RelativeLayout questionLayout = convertView.findViewById(R.id.rl_question);
        TextView questionText = (TextView) convertView.findViewById(R.id.tv_question);
        ImageView arrowImg = (ImageView) convertView.findViewById(R.id.img_arrow);
        TextView answerText = convertView.findViewById(R.id.tv_answer);

        questionText.setText(position+1+"、"+commonQuestion.getQuestion());
        answerText.setText(commonQuestion.getAnswer());
        if (openQuestionCache.contains(commonQuestion.getId())){
            answerText.setVisibility(View.VISIBLE);
            arrowImg.setImageResource(R.drawable.jx_ic_arr_up);
        }else {
            answerText.setVisibility(View.GONE);
            arrowImg.setImageResource(R.drawable.jx_ic_arr_down);
        }

        questionLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switchQuestion(commonQuestion.getId());
                notifyDataSetChanged();
            }
        });

        answerText.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (chatPresenter!=null){
                    AlertDialog.Builder builder = new AlertDialog.Builder(context);
                    AlertDialog alertDialog = builder.setItems(R.array.jx_text_message_operation_item2, new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            chatPresenter.copyTextMessage(commonQuestion.getAnswer());
                        }
                    }).create();
                    alertDialog.setCanceledOnTouchOutside(true);
                    alertDialog.show();
                }
                return false;
            }
        });

        return convertView;
    }

    public void refresh(List<JXCommonQuestion> orderList) {
        this.list.clear();
        this.list.addAll(orderList);
        notifyDataSetChanged();
    }

    public void switchQuestion(String id){
        //打开一个
        if (openQuestionCache.contains(id)){
            openQuestionCache.remove(id);
        }else {
            openQuestionCache.clear();
            openQuestionCache.add(id);
        }


        //打开多个
//        if (openQuestionCache.contains(id)){
//            openQuestionCache.remove(id);
//        }else {
//            openQuestionCache.add(id);
//        }
    }

    public void removeAllCache(){
        openQuestionCache.clear();
    }
}
